package rs.arm.letthemusicplay.utilities;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;

import android.os.Environment;
import android.util.Log;

public class FileUtilities {

	private static String TAG_FILE_UTILITIES = "ARM_FileUtilities";
	private static String PATH_TO_PLAYLIST_FOLDER = Environment.getExternalStorageDirectory()
			+ "/LetTheMusicPlay/Playlists";

	public static ArrayList<String> readSongsFromFileToArray(File f) {
		ArrayList<String> playList = new ArrayList<String>();

		if (f == null) {
			// if file is null we return empty playlist
			Log.e(TAG_FILE_UTILITIES, "File is null");
			return playList;
		}

		FileReader fileReader = null;
		BufferedReader br = null;
		try {
			fileReader = new FileReader(f);
			br = new BufferedReader(fileReader);
			String songPath;
			while ((songPath = br.readLine()) != null) {
				playList.add(songPath);
			}
		} catch (FileNotFoundException e) {
			Log.e(TAG_FILE_UTILITIES, "File" + f.getAbsolutePath() + " not found");
		} catch (IOException e) {
			Log.e(TAG_FILE_UTILITIES, "IO ERROR while reading" + f.getAbsolutePath());
		} finally {
			if (br != null) {
				try {
					br.close();
				} catch (IOException e) {
					Log.e(TAG_FILE_UTILITIES, "IO ERROR while closing" + f.getAbsolutePath());
				}
			}
		}
		return playList;
	}

	public static void writePlayListToFile(ArrayList<String> playList, String playListName) {
		try {
			// we will re-write old playlist with same name
			FileWriter fw = new FileWriter(PATH_TO_PLAYLIST_FOLDER + "\\" + playListName);
			for (int i = 0; i < playList.size(); i++) {
				fw.write(playList.get(i));
			}
			fw.close();
		} catch (IOException e) {
			Log.e(TAG_FILE_UTILITIES, "IO ERROR while creating" + PATH_TO_PLAYLIST_FOLDER + "\\"
											+ playListName);
		}
	}

	public static ArrayList<String> getPlaylists() {
		ArrayList<String> playLists = new ArrayList<String>();
		File playlistFolder = new File(PATH_TO_PLAYLIST_FOLDER);
		File[] list = playlistFolder.listFiles();
		for (int i = 0; i < list.length; i++) {
			playLists.add(list[i].getName());
		}
		Collections.sort(playLists);
		return playLists;
	}

	public static boolean isAnyPlaylistInFolder() {
		File playlistFolder = new File(PATH_TO_PLAYLIST_FOLDER);
		File[] list = playlistFolder.listFiles();
		if (list != null) {
			return list.length != 0;
		}
		return false;
	}

	public static ArrayList<String> getFavoritesPlaylist(){
		if(isAnyPlaylistInFolder()){
			ArrayList<String> playList = getPlaylists();
			if(playList.contains(Constants.FILE_NAME_FAVORITES)){
				File f = new File(PATH_TO_PLAYLIST_FOLDER + "/" + Constants.FILE_NAME_FAVORITES);
				return readSongsFromFileToArray(f);
			}
		}
		// return empty playlist if there is no favorite
		return new ArrayList<String>();
	}

	public static ArrayList<String> getSongsFromPlaylist(String playlistName){
		String filePath = PATH_TO_PLAYLIST_FOLDER + "/" + playlistName + ".txt";
		File f = new File(filePath);
		return readSongsFromFileToArray(f);
	}

}
